import {defineStore} from "pinia";
import {getAdminInfo, postLogin} from "@/api/admin";
import type {ILoginResponseData, TAdminInfo, TLoginForm} from "@/api/admin/type.ts";
import {GET_TOKEN, SET_TOKEN} from "@/utils";
type TState = {
    token:string | null,
    info:TAdminInfo | null
}
const useAdmin = defineStore("Admin",{
    state():TState{
        return {
            token:GET_TOKEN(),
            info:null
        }
    },
    actions:{
        // 登陆功能
        async postLoginAsync(loginForm:TLoginForm){
            // 调用登陆的接口
            const result:ILoginResponseData =  await postLogin(loginForm);
            // 设置localStorage
            SET_TOKEN(result.token);
            // 设置当前小仓库的数据状态token
            this.token = result.token;
            // 获取个人信息
            await this.getAdminInfoAsync();
        },
        // 获取管理员信息
        async getAdminInfoAsync(){
            const result = await getAdminInfo();
            this.info = result.info;
        }
    }
});
export default useAdmin;